#include <iostream>
#include <unistd.h>
#include <cerrno>
#include <cstring>
#include <sys/wait.h>
#include <sys/types.h>

const int size = 1024;
std::string GetProcessMessage()
{
    pid_t self_id = getpid();
    std::string sId = std::to_string(self_id);

    std::string message;
    message += " myId = ";
    message += sId;

    return message;
}
// 子进程进行写入
/*void SubProcessWrite(int wfd)
{
    static int cnt = 0;
    while (true)
    {
        std::string sCnt = std::to_string(cnt);
        cnt++;

        std::string info;
        info += sCnt;
        info += " I am your chile process ";
        info += GetProcessMessage();

        sleep(1);
        write(wfd, info.c_str(), info.size());
    }
    std::cout << "child process quit..." << std::endl;
}*/
// 子进程进行写入
void SubProcessWrite(int wfd)
{
    static int cnt = 0;
    while (true)
    {
        std::string sCnt = std::to_string(cnt);
        cnt++;

        std::string info;
        info += sCnt;
        info += " I am your chile process ";
        info += GetProcessMessage();

        write(wfd, info.c_str(), info.size());

        std::cerr << "#########################################" << std::endl;
        std::cerr << info << std::endl;
    }
    std::cout << "child process quit..." << std::endl;
}
// 父进程进行读取
void FatherProcessread(int rfd)
{
    char buffer[size];
    while (true)
    {
        sleep(2);
        ssize_t n = read(rfd, buffer, sizeof(buffer) - 1);
        if (n > 0) // 大于0读取成功s
        {
            buffer[n] = '\0';
            std::cout << "-------------------------------------------" << std::endl;
            std::cout << buffer << std::endl;
        }
        else if (n == 0) // 等于0写端关闭
        {
            std::cout << "write close father get return val is " << n;
            std::cout << " father quit too..." << std::endl;
            break;
        }
        else // 小于0证明读失败了
        {
            std::cout << "read error" << std::endl;
            break;
        }
    }
}

int main()
{
    // 1.pipe()接口创建管道
    int pipefd[2];
    int n = pipe(pipefd);
    if (n != 0)
    {
        std::cerr << errno << ":" << strerror(errno) << std::endl;
        exit(1);
    }
    std::cout << "pipefd[0]=" << pipefd[0] << "    " << "pipefd[1]=" << pipefd[1] << std::endl;
    // 2.fork()接口创建子进程
    pid_t id = fork();
    if (id == 0)
    {
        // 3.父子进程关闭不需要的文件描述符
        // 子进程write,关闭pipefd[0]
        close(pipefd[0]);
        std::cout << "子进程已关闭不需要的fd,准备发消息" << std::endl;
        sleep(1);

        //...父子进程通信
        SubProcessWrite(pipefd[1]);
        close(pipefd[1]);
        exit(0);
    }
    // 3.父子进程关闭不需要的文件描述符
    // 关闭父进程的写,关闭pipefd[1]
    close(pipefd[1]);
    //...父子进程通信
    std::cout << "父进程已关闭不需要的fd,准备收消息" << std::endl;
    sleep(1);
    FatherProcessread(pipefd[0]);

    // 父进程等待子进程
    int status = 0;
    pid_t rid = waitpid(id, &status, 0);
    if (rid > 0)
    {
        std::cout << "wait child process done" << std::endl;
        // 查看子进程的退出信息
        std::cout << "child exit sign is " << (status & 0x7f) << std::endl;
    }

    return 0;
}
